package com.socool.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.facebook.LoggingBehavior;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.Settings;
import com.socool.R;
import com.socool.Act_Tab;

public class Act_Login_FB extends Activity {
	// Facebook facebook = new Facebook("559816754030978");

	Session.StatusCallback statusCallback = new SessionStatusCallback();

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		// setContentView(R.layout.activity);

		Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);

		Session session = Session.getActiveSession();
		if (session == null) {
			Log.d("test", "session is null");
			if (savedInstanceState != null) {
				Log.d("test", "restoreSession");
				session = Session.restoreSession(this, null, statusCallback,
						savedInstanceState);
			}
			if (session == null) {
				Log.d("test", "new Session(this)");
				session = new Session(this);
			}
			Log.d("test", "setActiveSession");
			Session.setActiveSession(session);
			if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
				
				session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
			}
		}
		Log.d("test", "update");
		update();
	}

	private void update() {

		Session session = Session.getActiveSession();
		if (session.isOpened()) {
			Log.d("test", "session.isOpened()");
			Intent intent = new Intent(Act_Login_FB.this, Act_Tab.class);
			startActivity(intent);
			overridePendingTransition(R.anim.fade, R.anim.hold);

		} else {
			if (!session.isOpened() && !session.isClosed()) {
				
				
				session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
				
			} else {
				Session.openActiveSession(this, true, statusCallback);
			}
		}

	}

	@Override
	public void onStart() {
		super.onStart();
		Session.getActiveSession().addCallback(statusCallback);
	}

	@Override
	public void onStop() {
		super.onStop();
		Session.getActiveSession().removeCallback(statusCallback);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		Session.getActiveSession().onActivityResult(this, requestCode,
				resultCode, data);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);

		Session session = Session.getActiveSession();
		Session.saveSession(session, outState);
	}

	private class SessionStatusCallback implements Session.StatusCallback {
		@Override
		public void call(Session session, SessionState state,
				Exception exception) {
			
			update();

			/*
			 * Log.d("test", "Act_Login_FB   SessionStatusCallback call");
			 * Intent intent = new Intent(Act_Login_FB.this, FriendList.class);
			 * startActivity(intent);
			 * overridePendingTransition(R.anim.fade,R.anim.hold);
			 */
		}
	}
}
